package sebjul.ecom.servlet;

import java.io.*;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

import javax.ejb.*;

import sebjul.ecom.entities.*;
import sebjul.ecom.dao.*;


@WebServlet("/AddToCart")
public class AddToCart extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	@EJB
	ArticleDao articleDao;

    public AddToCart() {
        super();
        // TODO Auto-generated constructor stub
    }


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		Order order = (Order) session.getAttribute("cart");
		if (order == null) {
			order = new Order();
			session.setAttribute("cart", order);
		}
		Article article = articleDao.findArticleById(Integer.parseInt(request.getParameter("articleId")));
		if (order.getQuantity(article) == 0)
			order.setQuantity(article, 1);
		
		RequestDispatcher rd = request.getRequestDispatcher("cart.jsp");
		rd.forward(request, response);
		
	}
}
